Back to course home
0% completed
Vote For New Content
Meta Hard Problems
Sliding Window Median (hard)
Solution: Sliding Window Median
Alien Dictionary (hard)
Solution: Alien Dictionary
Minimum Window Substring (hard)
Solution: Minimum Window Substring
Best Meeting Point (hard)
Solution: Best Meeting Point
Maximum Sum of 3 Non-Overlapping Subarrays (hard)
Solution: Maximum Sum of 3 Non-Overlapping Subarrays
Making A Large Island (hard)
Solution: Making A Large Island
Basic Calculator (Hard)
Solution: Basic Calculator
Serialize and Deserialize Binary Tree (hard)
Solution: Serialize and Deserialize Binary Tree
Longest Increasing Path in a Matrix (hard)
Solution: Longest Increasing Path in a Matrix
Valid Palindrome III (hard)
Solution: Valid Palindrome III
Dot Product of Two Sparse Vectors (medium)
Problem Statement
Given two sparse vectors, efficiently compute the dot product of them.
A sparse vector is one in which most elements are zero.
Implement class Solution:
Solution(nums)Initializes the object with the vector numsdotProduct(vec)Calculates the dot product between thevecand instance ofSolution.
Examples
-
Example 1:
- Input: vec1 =
[1, 0, 0, 2], vec2 =[2, 3, 0, 1] - Expected Output:
4 - Justification: The dot product is
(1*2) + (0*3) + (0*0) + (2*1) = 2 + 0 + 0 + 2 = 4.
- Input: vec1 =
-
Example 2:
- Input: vec1 =
[0, 0, 0, 0], vec2 =[1, 1, 1, 1] - Expected Output:
0 - Justification: Since
vec1is all zeros, the dot product is0regardless ofvec2's values.
- Input: vec1 =
-
Example 3:
- Input: vec1 =
[1, 0, 0, 2, 3], vec2 =[0, 3, 0, 4, 0] - Expected Output:
8 - Justification: The dot product is
(1*0) + (0*3) + (0*0) + (2*4) + (3*0) = 8.
- Input: vec1 =
Constraints:
- n == nums1.length == nums2.length
- 1 <= n <= 105
- 0 <= nums1[i], nums2[i] <= 100
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Nested List Weight Sum
Solution: Dot Product of Two Sparse Vectors
Mark as Completed
On this page
Problem Statement
Examples
Try it yourself